class Admin::GeneralController < Admin::BaseController
  before_filter :unread_messages_amount, :only => [:index, :edit_school_moderator,
                                                   :school_trash_bin, :school_archives,
                                                   :school_manage, :area_manage]
  
  def index
  end
  
  def edit_school_moderator
    list_school_moderators
  end
  
  def search_school_moderator
    @users = User.find(:all, :conditions => ["email like ?", params[:email]])
    list_school_moderators
    render :action => 'edit_school_moderator'
  end
  
  # TODO merge with remove_school_moderator
  def update_school_moderator
    @user = User.find(params[:id])
    @user.is_school_admin = true
    @user.save!
    flash[:notice] = "将#{@user.login}(#{@user.email})加入了学校管理小组"
    list_school_moderators
    render :action => 'edit_school_moderator'
  end
  
  def remove_school_moderator
    @user = User.find(params[:id])
    @user.is_school_admin = false
    @user.save!
    flash[:notice] = "取消了#{@user.login}(#{@user.email})学校管理小组的权限"
    list_school_moderators
    render :action => 'edit_school_moderator'
  end
    
  def school_trash_bin
    @schools = School.find(:all, :conditions => {:is_hidden => true}, :order => "updated_at desc")
  end
  
  def rescue_school
    @school = School.find(params[:id])
    @school.update_attributes!(:is_hidden => false)
    redirect_to :action => "school_trash_bin"
  end
  
  def school_archives
    @schools = School.past_month
  end
  
  def book_manage
    @books = Book.all(params[:page])
  end
  
  def area_manage
    @areas = Area.roots
  end
  
  def group_manage
    @groups = Group.find(:all, :order => "groups.hidden asc, groups.position asc")
  end
  
  def statistics
  end
    
  private 
  def list_school_moderators
    @moderators = User.find(:all, :conditions => "is_school_admin=1")
  end
end
